package utils;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Threshresource;
import dtos.ThreshresourceDTO;


/**
 * Session Bean implementation class ThreshresourceDAO
 */
@Stateless
@LocalBean
public class ThreshresourceDAO implements ThreshresourceDAORemote {
	@PersistenceContext(unitName = "rmtPersistenceUnit")
	EntityManager em;
	
	@EJB
	Conv conv;

    /**
     * Default constructor. 
     */
    public ThreshresourceDAO() {
    }
    
    /**
     * Method used to insert treshresource 
     * @param techresource
     */
    @Override
    public void insertThreshresource(ThreshresourceDTO thresh) {
    	Threshresource t = em.find(Threshresource.class, thresh.getId());
    	if (t==null) {
    		Threshresource t1 = conv.fromEntityThreshresourceDTO(thresh);
    		em.persist(t1);
    	}
    	else {
    		em.merge(t);
    	}
    }
    
    /**
     * Method used to update threshresource
     * @param 
     */
    @Override
    public void updateThreshresource(ThreshresourceDTO ud){
    	if(em.find(Threshresource.class, ud.getId())!=null)
    	  em.merge(conv.fromEntityThreshresourceDTO(ud));
    }
    
    /**
     * Method used to get threshresource by id
     * @param id
     * @return threshresource
     */
    @Override
    public ThreshresourceDTO getThreshresource(int id) {
    	return conv.fromEntityThreshresource(em.find(Threshresource.class, id));
    }
    
    /**
     * Method used to deleteThreshresource
     * @param threshresource
     */
    @Override
    public void deleteThreshresurce(ThreshresourceDTO ud){
		Threshresource r = em.find(Threshresource.class, ud.getId());
        em.remove(r);
	}

}
